using DynamicProxies;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Reflection.Emit;

namespace DynamicProxies.Tests
{
	[TestClass]
	public sealed class ProxyContextTests
	{
		[TestMethod]
		public void CreateDefaultContext()
		{
			var context = new ProxyContext();
			Assert.IsFalse(context.Verify);
			Assert.IsFalse(context.GenerateDebugging);
			Assert.IsTrue((context.Access & AssemblyBuilderAccess.Run) == AssemblyBuilderAccess.Run);
			Assert.IsFalse((context.Access & AssemblyBuilderAccess.Save) == AssemblyBuilderAccess.Save);
		}

		[TestMethod]
		public void CreateSpecificContext()
		{
			var context = new ProxyContext(
				 AssemblyBuilderAccess.RunAndSave, true, true);
			Assert.IsTrue(context.Verify);
			Assert.IsTrue(context.GenerateDebugging);
			Assert.IsTrue((context.Access & AssemblyBuilderAccess.Run) == AssemblyBuilderAccess.Run);
			Assert.IsTrue((context.Access & AssemblyBuilderAccess.Save) == AssemblyBuilderAccess.Save);
		}

		[TestMethod]
		public void CreateNonVerifiableContext()
		{
			new ProxyContext(AssemblyBuilderAccess.RunAndSave, false, false);
		}

		[TestMethod, ExpectedException(typeof(ArgumentException))]
		public void CreateVerifiableContextWithTransientAccess()
		{
			new ProxyContext(AssemblyBuilderAccess.Run, true, false);
		}

		[TestMethod, ExpectedException(typeof(ArgumentException))]
		public void CreateReflectionOnlyContext()
		{
			new ProxyContext(AssemblyBuilderAccess.ReflectionOnly, true, false);
		}

		[TestMethod, ExpectedException(typeof(ArgumentException))]
		public void CreateSaveOnlyAccessContext()
		{
			new ProxyContext(AssemblyBuilderAccess.Save, true, false);
		}
	}
}
